const express = require('express')
const cors = require('cors')
const path = require('path')
const jwt = require('express-jwt')
const loginRouter = require(path.join(__dirname, 'routers', 'router-login.js'))
const userRouter = require(path.join(__dirname, 'routers', 'router-user.js'))
const app = express()

// 处理客户端请求post参数
// for parsing application/json
app.use(express.json()) 
// for parsing application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true })) 

// 解决跨域问题
app.use(cors())

// 验证token的有效性并解析token
// express-jwt在反解出token后，会自动向req对象中添加一个user属性，值就是从token中解析出的用户信息
app.use(jwt({ secret: 'bigevent' }).unless({ path: /^\/api/ }));

// 配置路由
app.use('/api', loginRouter)
app.use('/my', userRouter)

app.listen(3000, () => {
  console.log('running...')
})

app.get('/test', (req, res) => {
  res.send('test')
})